﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace CPPEI.Coolzon.UI.XProperty.Converters
{
    /*
     * double 
     * 和
     * <Double Value="1"/>
     * 之间相互转换
     * */
    public class XDoubleConverter : IPropertyValueConverter
    {
        public object Convert(ConvertContext context, Metadata.ValueNode valueNode)
        {
            double value = 0;
            if (double.TryParse(valueNode.GetAtrributeValue("value"), out value))
            {
                return value;
            }
            return null;
        }

        public Metadata.ValueNode ConvertBack(ConvertContext context, object value)
        {
            Metadata.ValueNode valueNode = new Metadata.ValueNode("Double");
            valueNode.Attributes = new Dictionary<string, string>();
            if (value == null)
            {
                valueNode.Attributes.Add("Value", "null");
            }
            else
            {
                valueNode.Attributes.Add("Value", System.Convert.ToString(value));
            }
            return valueNode;
        }
    }
}
